﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace BSC_OTC_Management.Forms
{
    public partial class Frm_NDDTCPH : Form
    {
        #region Khai bao bien
        Code.CL_Ultility _cl = new Code.CL_Ultility();
        Code.StaticVariable _code = new Code.StaticVariable();
        List<String> listNDD = new List<string>();
        DataTable _tableControl = new DataTable();
        DataTable _tableValue = new DataTable("tableValue");
        private string _function;
        private string _formName;
        string _ID = "";
        string _ID_ColumnName = "";
        string _formTitle = "";
        #endregion

        public Frm_NDDTCPH()
        {
            InitializeComponent();

        }

        public Frm_NDDTCPH(string formName, string formTitle, string function, string _ID, string _ID_ColumnName)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            this.gbTT.Enabled = true;
            btnFunction.Visible = true;
            this._function = function;
            this._formName = formName;
            this._formTitle = formTitle;
            this._tableControl = _code._services.GetFldMasterBy_Objname(_formName);
            if (_ID != "")
            {
                this._ID = _ID;
            }
            if (_ID_ColumnName != "")
            {
                this._ID_ColumnName = _ID_ColumnName;
            }

            //đổ dữ liệu cho combobox vai trò
            cbbVaitro();
            //kiểm tra xem đây là fuction nào?
            switch (this._function)
            {
                case "AddNew":
                    btnFunction.Text = "Thêm mới";
                    break;
                case "Edit":
                    btnFunction.Text = "Sửa";
                    ShowData();
                    break;
                case "View":
                    this.gbTT.Enabled = false;
                    btnFunction.Visible = false;
                    ShowData();
                    break;
                default:
                    break;

            }
        }

        private void cbbVaitro (){
            String sql = "SELECT CDVAL VALUECD, CDVAL VALUE, CDCONTENT DISPLAY FROM ALLCODE WHERE CDTYPE = 'VT' AND CDNAME = 'VAITRO' and cduser='Y' ORDER BY lstodr";
            DataTable result = _cl._services.GetDatatable_BySQL(sql);
            if (result.Rows.Count > 0){
                foreach (DataRow row in result.Rows){

                    cbb_vaitro.Items.Add(row["value"].ToString());
                }
            }
        }

        private void ShowData(){
            // lấy dữ liệu theo ID
            DataTable tableData = _code._services.GetDatatable_BySQL("select * from " + this._formName + " where " + this._ID_ColumnName + " like '" + this._ID + "'");
            if (tableData.Rows.Count > 0)
            {
                txt_sohd.Text = tableData.Rows[0]["sohd"].ToString();
                txt_tcph.Text = tableData.Rows[0]["matc"].ToString();
                SetNDDToCombobox(txt_tcph.Text);

                for (int i = 0; i < listNDD.Count; i ++){
                    if (listNDD[i] == tableData.Rows[0]["mandd"].ToString())
                        cbb_ndd.SelectedIndex = i;
                }
                for (int i = 0; i < cbb_vaitro.Items.Count; i++)
                {
                    if (cbb_vaitro.Items[i].ToString() == tableData.Rows[0]["vaitro"].ToString())
                        cbb_vaitro.SelectedIndex = i;
                }
                //cbb_ndd.SelectedText = tableData.Rows[0]["sohd"].ToString();
            }

        }

        private void txt_sohd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                try
                {
                    //chuyển sang Form View để tìm kiếm Sổ trái chủ
                    Forms.FrmView _FrmView = new Forms.FrmView("BSC_TBL_HOPDONGDAILY", "Thông tin Hợp đồng đại lý", true);
                    _FrmView.ShowDialog();
                    //lấy về dữ liệu DataTable formView Sotraichu
                    DataTable dt = Code.StaticVariable._tbl;
                    String sohd = "", tenhd = "", tcphid = "";
                    if (dt.Columns.Count >= 5 && dt.Rows.Count >= 1)
                    {
                        sohd = dt.Rows[0]["sohd"].ToString();
                        tenhd = dt.Rows[0]["tenhd"].ToString();
                        tcphid = dt.Rows[0]["tcphid"].ToString();
                        txt_sohd.Text = sohd;
                        txt_tcph.Text = tcphid;

                    }
                    Code.StaticVariable._tbl.Clear();
                    SetNDDToCombobox(tcphid);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        private void SetNDDToCombobox (string _tcphid){

            DataTable _tbl = new DataTable("tbl");
            String sql = "Select ndd.mandd, ndd.tenndd from bsc_tbl_nguoidaidien ndd where ndd.tcphid = '"+ _tcphid +"'";
            _tbl = _cl._services.GetDatatable_BySQL(sql);
            if (_tbl.Rows.Count >= 1)
            {
                for(int i = 0; i < _tbl.Rows.Count; i++)
                {
                    listNDD.Add(_tbl.Rows[i]["mandd"].ToString());
                    cbb_ndd.Items.Add(_tbl.Rows[i]["mandd"].ToString() + "-" + _tbl.Rows[i]["tenndd"].ToString());
                }
            }

        }

        private void btnFunction_Click(object sender, EventArgs e)
        {
            foreach (DataRow _row in this._tableControl.Rows)
            {
                this._tableValue.Columns.Add(_row["fldname"].ToString());
            }

            if (!Check_Null())
                return;
            int selectedNDD = cbb_ndd.SelectedIndex;
            _tableValue.Rows.Add(listNDD[selectedNDD], txt_tcph.Text, txt_sohd.Text, cbb_vaitro.Text);

            // Tạo xong row chưa dữ liệu, tạo thêm 1 row chứa kiểu dữ liệu
            DataRow rowDataType = _tableValue.NewRow();
            int stt = 0;
            foreach (DataRow _row in this._tableControl.Rows)
            {
                rowDataType[stt] = _row["fldtype"].ToString();
                stt++;
            }
            this._tableValue.Rows.Add(rowDataType);
            //string message box
            string msg = "";

            switch (this._function)
            {
                case "AddNew":
                    msg = _code._services.AddNewRow(this._formName, this._tableValue);
                    MessageBox.Show(msg, Code.StaticVariable.messeseboxLuuY, MessageBoxButtons.OK);
                    break;
                case "Edit":
                    msg = _code._services.EditRow(this._formName, this._tableValue, this._ID_ColumnName, this._ID, Code.StaticVariable.userID);
                    MessageBox.Show(msg, Code.StaticVariable.messeseboxLuuY, MessageBoxButtons.OK);
                    break;
                default:
                    break;
            }
            this.Close();
        }

        private bool Check_Null()
        {
            if (txt_tcph.Text.Length == 0 || txt_sohd.Text.Length == 0 || cbb_ndd.Text.Length == 0 || cbb_vaitro.Text.Length == 0 ){
                MessageBox.Show("Không được để trống dữ liệu!");
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
